package baidu

import (
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"net/http"
	url2 "net/url"
	"strings"
)

func OCR(imgBase64 string) {
	url := "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + GetAccessToken()
	payload := strings.NewReader(fmt.Sprintf("detect_direction=false&detect_language=false&paragraph=false&probability=false&image=%s", url2.QueryEscape(imgBase64)))
	client := &http.Client{}
	req, err := http.NewRequest("POST", url, payload)

	if err != nil {
		fmt.Println(err)
		return
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Add("Accept", "application/json")

	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(body))
}

// GetFileContentAsBase64FromPath
/**
 * 获取文件base64编码
 * @param string  path 文件路径
 * @return string base64编码信息，不带文件头
 */
func GetFileContentAsBase64FromPath(path string) string {
	srcByte, err := ioutil.ReadFile(path)
	if err != nil {
		fmt.Println(err)
		return ""
	}
	return base64.StdEncoding.EncodeToString(srcByte)
}
